package com.hornsun.client.mall.commodity.api;

import com.hornsun.client.mall.commodity.dto.*;
import com.hornsun.client.personalPage.service.AddressService;
import com.hornsun.domain.constant.types.InfoType;
import com.hornsun.domain.dbo.Commodity;
import com.hornsun.domain.dbo.ContentType;
import com.hornsun.domain.dbo.Evaluate;
import com.hornsun.repository.CommodityRepository;
import com.hornsun.repository.ContentTypeRepository;
import com.hornsun.repository.EvaluateRepository;
import com.hornsun.repository.FavouriteRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by 编译中... on 2018/3/2.
 */
@RestController
@RequestMapping("/api/commodity")
public class CommodityAPI {
    @Resource
    private ContentTypeRepository contentTypeRepository;

    @Resource
    private CommodityRepository commodityRepository;


    @Resource
    private EvaluateRepository evaluateRepository;

    @Resource
    private AddressService addressService;

    @Resource
    private FavouriteRepository favouriteRepository;



    //获取商城首页内容
    @GetMapping("/getMallInfo")
    public ResponseEntity<CommodityInfoDTO> getMallInfo(){
        List<ContentType> contentTypeList = contentTypeRepository.queryContentType(InfoType.COMMODITY.getType());
        if (contentTypeList.isEmpty()||contentTypeList.size()==0){
            return new ResponseEntity<CommodityInfoDTO>(HttpStatus.OK);
        }
        CommodityInfoDTO commodityInfoDTO = new CommodityInfoDTO();
        commodityInfoDTO.setTypes(contentTypeList);
        ContentType type = contentTypeList.get(0);
        int page = 1;
        commodityInfoDTO.setCommodities(commodityRepository.queryCommodityInfo(type.getId(),page));
        commodityInfoDTO.setTotalCount(commodityRepository.getCountByTypeid(type.getId()));
        return new ResponseEntity<CommodityInfoDTO>(commodityInfoDTO,HttpStatus.OK);
    }

    //获取更多商城信息
    @GetMapping("/getMoreMallInfo")
    public ResponseEntity<CommodityDTO> getMoreMallInfo(@RequestParam("typeid")String typeid , @RequestParam("page")Integer page){
        CommodityDTO moreCommodityDTO = new CommodityDTO();
        moreCommodityDTO.setCommodityList(commodityRepository.queryCommodityInfo(typeid,page));
        moreCommodityDTO.setTotalCount(commodityRepository.getCountByTypeid(typeid));
        return new ResponseEntity<CommodityDTO>(moreCommodityDTO,HttpStatus.OK);
    }

    //获取商品详情
    @GetMapping("/getCommodityDetail")
    public ResponseEntity<CommodityDetailDTO> getCommodityDetail(HttpServletRequest httpRequest,@RequestParam("guid")String commodityGuid){
        String openid = httpRequest.getAttribute("openid").toString();
        CommodityDetailDTO commodityDetailDTO = new CommodityDetailDTO();
        commodityDetailDTO.setCommodity(commodityRepository.getCommodityByGuid(commodityGuid));
        commodityDetailDTO.setTotalCount(evaluateRepository.getTotalCount(commodityGuid));
        commodityDetailDTO.setCheckFavourite(favouriteRepository.checkFavourite(openid,commodityGuid));
        //评价显示第一页
        int page = 1;
        commodityDetailDTO.setEvaluateList(evaluateRepository.selectEvaluate(commodityGuid,page));
        return new ResponseEntity<CommodityDetailDTO>(commodityDetailDTO,HttpStatus.OK);
    }

    //获取跟多的评价
    @GetMapping("/getMoreEvaluate")
    public ResponseEntity<List<Evaluate>> getMoreEvalute(@RequestParam("guid")String commodityGuid,@RequestParam("page")int page){
        return new ResponseEntity<List<Evaluate>>(evaluateRepository.selectEvaluate(commodityGuid,page),HttpStatus.OK);
    }


    //获取支付页面信息
    @GetMapping("/getPayInfo")
    public ResponseEntity<PayInfoDTO> getPayInfo(HttpServletRequest httpRequest, @RequestParam("guid")String commdityGuid){
        String openid = httpRequest.getAttribute("openid").toString();
        PayInfoDTO payInfoDTO = new PayInfoDTO();
        payInfoDTO.setCommodity(commodityRepository.getCommodityByGuid(commdityGuid));
        payInfoDTO.setAddress(addressService.getPayAddress(openid));
        return new ResponseEntity<PayInfoDTO>(payInfoDTO,HttpStatus.OK);
    }


    //评论
    @Transactional
    @PostMapping("/evaluate")
    public ResponseEntity<?>  evaluate(EvaluateDTO evaluateDTO){
        Evaluate evaluate = new Evaluate();
        evaluate.setEvaluateGuid(UUID.randomUUID().toString());
        evaluate.setCommodityGuid(evaluateDTO.getCommodityGuid());
        evaluate.setContent(evaluateDTO.getContent());
        evaluate.setNickname(evaluateDTO.getNickname());
        evaluate.setStarLevel(evaluateDTO.getStarLevel());
        evaluate.setTime(new Date());
        evaluateRepository.insertEvaluate(evaluate);
        Commodity commodity = new Commodity();
        commodity.setCommodityGuid(evaluateDTO.getCommodityGuid());
        commodity.setPoint(evaluateRepository.getAVGPoint(evaluateDTO.getCommodityGuid()));
        commodityRepository.updateCommodity(commodity);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }



}
